/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.controller
{
	import flash.errors.IllegalOperationError;
	
	import org.etherframework.IEther;
	import org.etherframework.controller.events.CommandErrorEvent;
	import org.etherframework.common.command.ICommand;
	import org.etherframework.core.common.ether_internal;
	
	use namespace ether_internal;

	public class EtherCommand implements ICommand
	{
		private var _ether:IEther;
		private var _errorDispatched:Error;
		private var _destroyed:Boolean;
		
		public function execute():void
		{
			throw new IllegalOperationError("Method must be overridden.");
		}
		
		protected function dispatchError(error:Error):void
		{
			if (_destroyed || _errorDispatched)
				return;
			
			_errorDispatched = error;
			
			// If there is no listener for this event it will be 
			// interpreted as uncaught error with appearing of system error 
			// log window in context of debugger version of FlashPlayer.
			ether.dispatch(new CommandErrorEvent(error, this));
		}
		
		public function get ether():IEther 
		{ 
			if (!_ether)
				throw new Error("EtherCommand " + this + " is not properly processed.");
			
			return _ether;
		}
		
		public function get errorDispatched():Error { return _errorDispatched; }
		
		ether_internal final function __init(ether:IEther):void
		{
			_ether = ether;
		}
	}
}